package com.oauth2.config;

import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.DefaultAccessTokenConverter;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 自定义jwtToken 扩展
 */
@Component
public class MyAccessTokeConvoter extends DefaultAccessTokenConverter {
    @Override
    public Map<String, ?> convertAccessToken(OAuth2AccessToken token, OAuth2Authentication authentication) {
        Map<String, String> stringMap = (Map<String, String>) super.convertAccessToken(token, authentication);
    HttpServletRequest request = (HttpServletRequest) RequestContextHolder.getRequestAttributes();
        String remoteHost = request.getRemoteAddr();
        stringMap.put("clientIp",remoteHost);

        return stringMap;
    }
}
